<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Devices</title>
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='control_style.css') }}"/>
</head>
<body>
<div id="" class="left full-height">
    <!-- logo -->
    <div id="" class="logo-bar">
        <div id="" class="ele full-height">
            <h1 style="color: #ededed;">LOGO</h1>
        </div>

    </div>
    <!-- tabs -->
    <div id="" class="tab-bar">
        <div style="height: 50px;width: 100%"></div>
        <a href="/devices">
            <div id="" class="tab-content choix">
                DEVICES
            </div>
        </a>
        <a href="/types">
            <div id="" class="tab-content no-choix">
                TYPE
            </div>
        </a>
        <a href="/conn">
            <div id="" class="tab-content no-choix">
                CONNECTIONS
            </div>
        </a>
        <a href="/log">
            <div id="" class="tab-content no-choix">
                LOG
            </div>
        </a>
        <a href="/groups">
            <div id="" class="tab-content no-choix">
                GROUPS
            </div>
        </a>
    </div>
</div>

<div id="" class="right full-height">
    <!-- admin -->
    <div id="" class="admin-bar">

    </div>
    <!-- main content -->
    <div id="" class="content-background">
        <div class="content-box">
            <div class="data-box" style="padding-left: 30px">
                <div id="device_function"></div>
            </div>

        </div>

    </div>
</div>
</body>
<script>
    var dict_t = {{ td2|tojson }};

    function jump(url) {
        window.location.href = url;
    }

    function create_xhr() {
        let XHR = [  //兼容不同浏览器和版本得创建函数数组
            function () {
                return new XMLHttpRequest()
            },
            function () {
                return new ActiveXObject("Msxml2.XMLHTTP")
            },
            function () {
                return new ActiveXObject("Msxml3.XMLHTTP")
            },
            function () {
                return new ActiveXObject("Microsoft.XMLHTTP")
            }
        ];
        let xhr = null;
        //尝试调用函数，如果成功则返回XMLHttpRequest对象，否则继续尝试
        for (let i = 0; i < XHR.length; i++) {
            try {
                xhr = XHR[i]();
            } catch (e) {
                continue  //如果发生异常，则继续下一个函数调用
            }
            break;  //如果成功，则中止循环
        }
        return xhr;  //返回对象实例
    }

    device_function = document.getElementById('device_function')
    modify_device("{{ device_id }}", "{{ device_name }}", "{{ td[device_type] }}", "{{ device_addr }}")


    function modify_device(device_id, device_name, device_type, device_addr) {
        device_function.innerHTML = "<h2>Modify</h2>\n" +
            "        <p>id:<span id=\"modify-device-id\">" + device_id + "</span></p>\n" +
            "        <p>name:<input type=\"text\" id=\"modify-device-name\"value=" + device_name + " style=\"width:250px\"></p>\n" +
            "        <p>type:\n" +
            "            <select name=\"\" id=\"modify-device-type\">\n" +
            "                {% for t in td %}\n"+
                "                    <option value=\"{{ td[t] }}\">{{ td[t] }}</option>\n"+
                "                {% endfor %}\n" +
            "            </select></p>\n" +
            "        <p>address:<input type=\"text\" id=\"modify-device-addr\" value=" + device_addr + " style=\"width:250px\"></p>\n" +
            "        <button onclick=\"confirm_modify_device()\">submit</button>"

        let d_type_block = document.getElementById('modify-device-type')
        makeOptionSelected(d_type_block, device_type)

    }

    function makeOptionSelected(selectBlock, optionValue) {
        for (let i = 0; i < selectBlock.options.length; i++) {
            if (selectBlock.options[i].value == optionValue) {
                selectBlock.options[i].selected = true
            }
        }
    }

    function getSelectValue(selectBlock) {
        let s_num = selectBlock.selectedIndex
        let s_value = selectBlock.options[s_num].value
        return s_value
    }

    function confirm_modify_device() {
        let d_id = document.getElementById('modify-device-id').innerText

        let d_name = document.getElementById('modify-device-name').value

        let d_type_block = document.getElementById('modify-device-type')
        let d_type_num = d_type_block.selectedIndex
        let d_type = d_type_block.options[d_type_num].value

        let d_addr = document.getElementById('modify-device-addr').value

        let command =
            {
                "id": d_id,
                "name": d_name,
                "type_id": dict_t[d_type],
                "addr": d_addr
            }

        let xhr = create_xhr();
        xhr.open("POST", "/equipment/updateEquipment", true)
        xhr.onreadystatechange = function () {  //绑定响应状态事件监听函数
            if (xhr.readyState == 4) {  //监听readyState状态
                if (xhr.status == 200 || xhr.status == 0) {  //监听HTTP状态码
                    let result = xhr.responseText //接收数据
                    console.log(result);
                    if (result == "success") {
                        {#location.reload();#}
                        jump('/devices')
                    } else {
                        alert(result)
                    }
                }
            }
        }
        xhr.send(JSON.stringify(command))
    }


</script>
</html>